(function () {
	var myScroll;//定义全局变量
	var page = 1;//页数

	var refresher = {
		info: {
			"pullDownLable": "下拉刷新",
			"pullingDownLable": "释放刷新",
			"pullUpLable": "上拉加载",
			"pullingUpLable": "释放加载",
			"loadingLable": "Loading..."
		},
		init: function (parameter) {
			var wrapper = document.getElementById(parameter.id);

			var div = document.createElement("div");
			div.id = "scroller";
			wrapper.appendChild(div);

			var scroller = document.getElementById("scroller");
			var list = document.querySelector("#" + parameter.id + " ul");
			scroller.insertBefore(list, scroller.childNodes[0]);

			var pullDown = document.createElement("div");
			pullDown.id = "pullDown";
			var loader = document.createElement("div");
			loader.className = "loader";
			for (var i = 0; i < 4; i++) {
				var span = document.createElement("span");
				loader.appendChild(span);
			}
			pullDown.appendChild(loader);

			var pullDownLabel = document.createElement("div");
			pullDownLabel.className = "pullDownLabel";
			pullDown.appendChild(pullDownLabel);

			scroller.insertBefore(pullDown, scroller.childNodes[0]);

			var pullUp = document.createElement("div");
			pullUp.id = "pullUp";
			var loader = document.createElement("div");
			loader.className = "loader";
			for (var i = 0; i < 4; i++) {
				var span = document.createElement("span");
				loader.appendChild(span);
			}
			pullUp.appendChild(loader);

			var pullUpLabel = document.createElement("div");
			pullUpLabel.className = "pullUpLabel";
			var content = document.createTextNode(refresher.info.pullUpLable);
			pullUpLabel.appendChild(content);
			pullUp.appendChild(pullUpLabel);

			scroller.appendChild(pullUp);
			//create dom above
			//create dom ,you can wirte it yourself
			var pullDownEl = document.getElementById('pullDown');
			var pullDownOffset = pullDownEl.offsetHeight;
			var pullUpEl = document.getElementById('pullUp');
			var pullUpOffset = pullUpEl.offsetHeight;
			//parameter
			this.scrollIt(parameter, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset);
		},

		scrollIt: function (parameter, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset) {
			myScroll = new iScroll(parameter.id, {
				useTransition: true,
				vScrollbar: false, //hide the iscroll v bar
				topOffset: pullDownOffset,
				onRefresh: function () {
					refresher.onRelease(pullDownEl, pullUpEl);
				},
				onScrollMove: function () {
					refresher.onScrolling(this, pullDownEl, pullUpEl, pullUpOffset);//element
				},
				onScrollEnd: function () {
					refresher.onPulling(pullDownEl, parameter.pullDownAction, pullUpEl, parameter.pullUpAction);
				},
			});
			setTimeout(function () {
				pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable
			}, 300);
			document.addEventListener('touchmove', function (e) {
				e.preventDefault();
			}, false);
		},

		//things loader css on scrolling,you can wirte it yourself
		onScrolling: function (e, pullDownEl, pullUpEl, pullUpOffset) {

			if (e.y > -(pullUpOffset)) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable;
				e.minScrollY = -pullUpOffset;
			}
			if (e.y > 0) {
				pullDownEl.className = 'flip';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullingDownLable;
				e.minScrollY = 0;
			}
			if (e.scrollerH < e.wrapperH && e.y < (e.minScrollY - pullUpOffset) || e.scrollerH > e.wrapperH && e.y < (e.maxScrollY - pullUpOffset)) {
				document.getElementById("pullUp").style.display = "block";
				pullUpEl.className = 'flip';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullingUpLable;
			}
			if (e.scrollerH < e.wrapperH && e.y > (e.minScrollY - pullUpOffset) && pullUpEl.className.match('flip') || e.scrollerH > e.wrapperH && e.y > (e.maxScrollY - pullUpOffset) && pullUpEl.className.match('flip')) {
				document.getElementById("pullUp").style.display = "none";
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullUpLable;
			}
		},

		//things loader css on release,you can wirte it yourself
		onRelease: function (pullDownEl, pullUpEl) {
			if (pullDownEl.className.match('loading')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable;
				pullDownEl.querySelector('.loader').style.display = "none"
				pullDownEl.style.lineHeight = pullDownEl.offsetHeight + "px";
			}
			if (pullUpEl.className.match('loading')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullUpLable;
				pullUpEl.querySelector('.loader').style.display = "none"
				pullUpEl.style.lineHeight = pullDownEl.offsetHeight + "px";
			}
		},

		//things loader css on pulling,you can wirte it yourself
		onPulling: function (pullDownEl, pullDownAction, pullUpEl, pullUpAction) {
			if (pullDownEl.className.match('flip')) {
				pullDownEl.className = 'loading';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.loadingLable;
				pullDownEl.querySelector('.loader').style.display = "block"
				pullDownEl.style.lineHeight = "20px";
				if (pullDownAction)
					pullDownAction();	// Execute custom function (ajax call?)
			}
			if (pullUpEl.className.match('flip')) {
				pullUpEl.className = 'loading';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.loadingLable;
				pullUpEl.querySelector('.loader').style.display = "block"
				pullUpEl.style.lineHeight = "20px";
				if (pullDownAction)
					pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	};

	//设置wrapper高度
	set_wrapper_height();

	function set_wrapper_height() {
		$('#wrapper').height($(window).height() - $('.nav').outerHeight() - $('.container').outerHeight());
		$('#wrapper_search').height($(window).height() - $('.nav').outerHeight() - $('.container').outerHeight()).css('overflow', 'auto');
	}

	//获取充值列表数据
	get_recharge_list_data('1', '20', function (data) {
		recharge_list_fill(data, 'recharge_list', function () {
			refresher_init();//初始化
		});
	});

	function get_recharge_list_data(page, perpage, callbak) {
		$.ajax({
			url: "/index/rechargeList",
			type: "post",
			dataType: "json",
			data: {
				page: page,
				perpage: perpage
			},
			success: function (data) {
				if (data.code == '1') {
					if (callbak) {
						callbak(data.data);
					}
				}
			},
			error: function (err) {
				console.log(err);
			}
		})
	}

	//生成充值列表 并填充到页面
	function recharge_list_fill(data, container, callback) {
		var is_check = '';//-1:已取消 0:未审核 1:已审核
		var is_check_bool = '';
		for (var i = 0, len = data.length; i < len; i++) {
			if (data[i].is_check == '-1') {
				is_check = '已取消';
				is_check_bool = '';
			}
			if (data[i].is_check == '0') {
				is_check = '未审核';
				is_check_bool = 'active';
			}
			if (data[i].is_check == '1') {
				is_check = '已审核';
				is_check_bool = '';
			}

			var html = '';
			html += '<li class="recharge_item" recharge_id="' + data[i].recharge_id + '">';
			html += '<div class="clearfix">';
			html += '<div class="pull-left">序号: ' + data[i].recharge_id + '</div>';
			html += '<div class="pull-right ' + is_check_bool + '">' + is_check + '</div>';
			html += '</div>';
			html += '<div class="clearfix">';
			html += '<div class="pull-left">充值人: ' + data[i].user_name + '</div>';
			html += '<div class="pull-right"><img src="/statics/images/more.png" alt=""></div>';
			html += '</div>';
			html += '<div class="clearfix">';
			html += '<div class="pull-left">手机号: ' + data[i].user_mobile + '</div>';
			html += '<div class="pull-right">+' + data[i].recharge_money + '</div>';
			html += '</div>';
			html += '</li>';

			$('#' + container).append(html);
		}
		if (callback) {
			callback();
		}
	}

	//初始化
	function refresher_init() {
		refresher.init({
			id: "wrapper",
			pullDownAction: Refresh,
			pullUpAction: Load
		});
	}

	//刷新
	function Refresh() {
		setTimeout(function () {
			page = 1;
			$.ajax({
				url: "/index/rechargeList",
				type: "post",
				dataType: "json",
				data: {
					page: 1,
					perpage: 20
				},
				success: function (data) {
					if (data.code == '1') {
						$('#recharge_list').empty();
						recharge_list_fill(data.data, 'recharge_list', function () {
							myScroll.refresh();
						});
					}
				},
				error: function (err) {
					console.log(err);
				}
			});
		}, 1000);
	}

	//加载
	function Load() {
		setTimeout(function () {
			page++;
			$.ajax({
				url: "/index/rechargeList",
				type: "post",
				dataType: "json",
				data: {
					page: page,
					perpage: 20
				},
				success: function (data) {
					if (data.code == '1') {
						recharge_list_fill(data.data, 'recharge_list', function () {
							myScroll.refresh();
						});
					}else {
						myScroll.refresh();
						$('.pullUpLabel').text('暂无更多数据...');
					}
				},
				error: function (err) {
					console.log(err);
				}
			});
		}, 1000);
	}

	/* *******************************************************
	*                   页面跳转
	* ********************************************************/
	skip_page('recharge_list');
	skip_page('recharge_list_search');
	function skip_page(container) {
		$('#'+container).delegate('.recharge_item','click',function () {
			window.location = '/index/recharge_info?recharge_id='+$(this).attr('recharge_id')
		})
	}

	/*
	*    查询模块
	* */
	//手机号查询input获取焦点
	phone_number_no_get_focus();

	function phone_number_no_get_focus() {
		var phone_number_no = $('#phone_number_no');
		phone_number_no.on('focus', function () {
			$('.no_active_focus').hide();
			$('.active_focus').show();
			$('#phone_number').focus();
			$('#wrapper').hide();
			$('#wrapper_search').show();

			get_recharge_list_data('1', '2000', function (data) {
				recharge_list_fill(data, 'recharge_list_search', function () {
					refresher_init();//初始化
					if (data) {
						phone_number_keydown(data, $('#phone_number'));//查询数组
					}
					phone_number_keyup(data);//查询搜索框键盘事件监听
				});
			});//获取充值列表数据
		});
	}

	//点击取消
	cancel_btn_click();

	function cancel_btn_click() {
		var btn = $('#phone_number_cancel');
		btn.click(function () {
			$('.active_focus').hide();
			$('.no_active_focus').show();
			$('#wrapper_search').hide();
			$('#wrapper').show();

			//获取充值列表数据
			get_recharge_list_data('1', '20', function (data) {
				recharge_list_fill(data, 'recharge_list', function () {
					refresher_init();//初始化
				});
			});
		});
	}

	//点击X清除输入框val
	clear_input_val();

	function clear_input_val() {
		var clear_input_val = $('#clear_input_val');
		clear_input_val.click(function () {
			$('#phone_number').val('').focus();
		});

		clear_input_val.prev().click(function () {
			$('#phone_number').val('').focus();
		})
	}

	//查询搜索框键盘事件监听
	function phone_number_keyup(data) {
		$('#phone_number').unbind('keyup');
		$('#phone_number').on('keyup', function () {
			phone_number_keydown(data, $(this));//查询数组
		});
	}

	//查询数组
	function phone_number_keydown(arr, this_) {
		var new_arr = [];
		for (var i = 0, len = arr.length; i < len; i++) {
			var val = arr[i].user_mobile;
			if (val.substring(0, val.length).indexOf(this_.val()) == 0) {
				new_arr.push(arr[i]);
			}
		}
		$('#recharge_list_search').empty();
		recharge_list_fill(new_arr, 'recharge_list_search');
	}

	/* *****************************************************
	*                返回上一页
	* ******************************************************/
	go_back();
	function go_back() {
		$('#back').click(function () {
			history.go(-1);
		});
	}

})();